using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for bzUserGroup
/// </summary>

public class bzUserGroup
{
	private dbConnection _DB = null;
    private string _TableName = "bzAdminUserGroup";
    private string _IDType = "_NVARCHAR";
    private string _IDCol = "GroupUser";
    private string _TableChild = "bzAdminUser";
    public bzUserGroup()
	{
        _DB = new dbConnection();
	}
    public DataTable getTable(string sKey,string sEnabled,string sPage,string sLimit)
    {       
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@Key", "%"+sKey+"%");
        p[1] = new SqlParameter("@Enabled",  sEnabled );
        p[2] = new SqlParameter("@Page", sPage);
        p[3] = new SqlParameter("@Limit", sLimit);       
        return _DB.getDataTable(_TableName+"_search",p);
       
    }
    public long getTotal(string sKey,string sEnabled)
    {
        SqlParameter[] p = new SqlParameter[2];        
        p[0] = new SqlParameter("@Key", "%" + sKey + "%");
        p[1] = new SqlParameter("@Enabled", sEnabled);
        return _DB.getTotalTable(_TableName+"_getTotal",p);
    }
    public DataTable getRecord(string ID)
    {
        return this.getRecord(ID, "-1");
    }
    public DataTable getRecord(string ID, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@Enabled", Enabled);
        return _DB.getDataTable("bz_getRecord" + _IDType, p);
    } 
    public int deleteRecord(string[] sID)
    {
        int count = 0;
        foreach (string str in sID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@TableChild", _TableChild);
            p[2] = new SqlParameter("@IDCol", _IDCol);
            p[3] = new SqlParameter("@IDValue", str);
            count += _DB.updateData("bz_deleteRecord" + _IDType, p);
        }
        return count;
    }
    public int activeRecord(string[] sID, string sEnabled)
    {
        int count = 0;
        foreach (string str in sID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", str);
            p[3] = new SqlParameter("@Enabled", sEnabled);
            count += _DB.updateData("bz_activeRecord" + _IDType, p);
        }
        return count;
    }  
      
    public int insertRecord(string sGroupUser, string sAdminLevel, string sEnabled)
    {
        string sCode = bzStringClass.MD5(sGroupUser.Trim().ToLower() + sAdminLevel);
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@GroupUser", sGroupUser);
        p[1] = new SqlParameter("@AdminLevel", sAdminLevel);
        p[2] = new SqlParameter("@Code", sCode);     
        p[3] = new SqlParameter("@Enabled", sEnabled);       
        return _DB.updateData(_TableName+"_insert",p);
    }
    public int updateRecord(string sGroupUser, string sAdminLevel, string sEnabled)
    {
        string sCode = bzStringClass.MD5(sGroupUser.Trim().ToLower() + sAdminLevel);
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@GroupUser", sGroupUser);
        p[1] = new SqlParameter("@AdminLevel", sAdminLevel);
        p[2] = new SqlParameter("@Code", sCode);
        p[3] = new SqlParameter("@Enabled", sEnabled);
        return _DB.updateData(_TableName + "_update", p);
    }   
}
